<?php
function_exists('UnixPath')  || die('OK');

	$ACT = array('list', 'add', 'aok', 'edit', 'eok', 'del');
	in_array($_GET['act'], $ACT) || $_GET['act'] = "list";
	//$html['tpl_right_act'] = $_GET['act'];	

	$T	= new Tab(2);

	$_GET['id']	= (int) $_GET['id'];
	$_GET['pid']	= (int) $_GET['pid'];

	//$_PA	= require("./tree/PA.php");
	$_PB	= require(B_DIR . '/_res/tree/PB.php');

	if( empty($_PB[$_GET['pid']]) && $_GET['pid'] !== 1 ){
		A::msg('栏目不存在！');
	}

	$P	= $_PB[$_GET['pid']];


	$tab = $T->attrs['tableName'];

	$body['navlist'] = array(
		'list' =>	$T->attrs['itemName'] . '列表',
		'add'	=>	'添加' . $T->attrs['itemName']
	);


	$body['_url']	= '&pid=' .  $_GET['pid'] . '&';
	$body['url']	= $html['url'] . $body['_url'];
	$body['node_tree']	= title_tree($P[1]) ;

if( is_array($P[2])) {
	$body['node_tree'] .= "<small>&#187;</small> <a href=\"?mod=$_GET[mod]&fun=column&act=list&pid=$_GET[pid]\" >$P[0]</a> <small>&#187;</small> <a href=\"?mod=$_GET[mod]&fun=$_GET[fun]&act=list&pid=$_GET[pid]\" style=\"color:red;\">区域列表</a>";
}else {	//px($P);
	if( $_GET[pid]>1) $body['node_tree'] .= "<small>&gt;&gt;</small> <a>$P[0]</a>";
	$body['node_tree'] .= " <small>&#187;</small> <a href=\"?mod=$_GET[mod]&fun=$_GET[fun]&act=list&pid=$_GET[pid]\" style=\"color:red;\">区域列表</a>";
}

	if( $_GET['pid']>1 ){	
		$body['navslist'] = array(
			array('编辑栏目', '?mod=' . $_GET['mod'] . '&fun=column&act=edit&pid=' . $P[1] . '&id=' . $_GET['pid'], $P[0])
		);
	}

if( $_GET['act'] === 'aok' ){
	$_POST['cid']	= $_GET['pid'];
	$id	= $T->insert('cid');
	advert_cache($id);
	A::msg('添加 ' . $T->attrs['itemName'] . ' ' . $_POST['flag'] . ' 完成!', $_POST['jurl'], 1, 1 );
}


if( $_GET['act']=='edit' ){
	$L	= $T->one($_GET['id']);
}

if( $_GET['act'] === 'eok' ){
	$T->update('', $_GET['id']);
	advert_cache($_GET['id']);
	A::msg('编辑 ' . $T->attrs['itemName'] . ' ' . $_POST['flag'] . ' 完成!', $_POST['jurl'], 1, 1 );
}

if( $_GET['act'] === 'list' ){	

	$L = $T->page('*', 'cid=' .$_GET['pid'] . ' ORDER BY atype ASC, id ASC');


	$Xcolor = array( 8=>'369', 9=>'963', 10=>'693' );
	$tpl->assign_by_ref('Xcolor', $Xcolor);
}

if( $_GET['act'] === 'del' ){
	$L	= $T->delete($_GET['id']);
	@unlink(B_DIR . '/_res/_tpl/area/'. $L['id'] . '.txt');
	A::msg('删除 ' . $T->attrs['itemName'] . ' ' . $L['flag'] . ' 完成!', $_POST['jurl'], 1, 1 );
}



$tpl->assign_by_ref("T", $T->attrs);
$tpl->assign_by_ref("F", $T->attrs['field']);
$tpl->assign_by_ref("P", $P);
$tpl->assign_by_ref("xL", $xL);




/***********************************************************************************************/



function title_tree($id){	
	global $_PB;
	if($id<=1){		
		$isc = $_GET['pid']=='1'?" style=\"color:red;\"":'';
		$str	= "<a href=\"?mod=$_GET[mod]&fun=column&act=list&pid=1\"$isc>首页</a> ";
	}else{
		$str	= title_tree($_PB[$id][1])."<small>&gt;&gt;</small> <a href=\"?mod=$_GET[mod]&fun=column&act=list&pid=$id\">".$_PB[$id][0]."</a> ";
	}
	return $str;
}

function advert_cache($id){
	global $T;
	$L	= $T->one($id);
	if( empty( $L) ){
		var_dump($id);
		die('Error On File:' . __FILE__ . ' Line:' . __LINE__);
	}
	$L['area']	= str_replace('%7B',  '{', $L['area']);
	$L['area']	= str_replace('%7D',  '}', $L['area']);
	//$L['area']	= str_replace('&lt;/&gt;', "",$L['area']);
	$flag	= str_replace('<', '&lt;',	$L['flag']);
	$flag	= str_replace('>', '&gt;',	$flag);
	$str	= '<!-- Area:' . $L['id'] . ':' . $flag . ' -->'. $L['area'] . '<!-- /Area:' .  $L['id'] . ':' . $flag . ' -->';
	file_put_contents( B_DIR . '/_res/_tpl/area/' . $L['id']  . '.txt', $str);

}